package com.syf.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syf.domain.CoinType;
import com.syf.model.R;
import com.syf.service.CoinTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.mapstruct.MapMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author 苏耀奋
 * @create 2021-10-13 20:05
 */
@RestController
@RequestMapping("/coinTypes")
@Api(value = "币种类型的控制器")
public class CoinTypeController {

    @Autowired
    private CoinTypeService coinTypeService;


    //-------------------后台【币种配置】--------------------------------------
    @GetMapping
    @ApiOperation(value = "条件分页查询-币种类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),
            @ApiImplicitParam(name = "code", value = "币种类型")
    })
    @PreAuthorize("hasAuthority('trade_coin_type_query')")
    public R<Page<CoinType>> fingByPage(@ApiIgnore Page<CoinType> page, String code){
        page.addOrder(OrderItem.desc("last_update_time"));  //排序
        Page<CoinType> coinTypePage = coinTypeService.fingByPage(page,code);
        return R.ok(coinTypePage);
    }



    @PostMapping
    @ApiOperation(value = "新增币种类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coinType", value = "coinType 的json")
    })
    @PreAuthorize("hasAuthority('trade_coin_type_create')")
    public R add(@RequestBody @Validated CoinType coinType){
        boolean save = coinTypeService.save(coinType);
        if(save){
            return R.ok();
        }
        return R.fail("新增成功");
    }


    @PatchMapping
    @ApiOperation(value = "修改币种类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coinType", value = "coinType 的json")
    })
    @PreAuthorize("hasAuthority('trade_coin_type_update')")
    public R update(@RequestBody @Validated CoinType coinType){
        boolean update = coinTypeService.updateById(coinType);
        if(update){
            return R.ok();
        }
        return R.fail("修改失败");
    }


    @PostMapping("/setStatus")
    @ApiOperation(value = "修改币种类型的状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "coinType的id"),
            @ApiImplicitParam(name = "coinType", value = "coinType 的json")
    })
    @PreAuthorize("hasAuthority('trade_coin_type_update')")
    public R setStatusById(@RequestBody  CoinType coinType){
        boolean update = coinTypeService.updateById(coinType);
        if(update){
            return R.ok();
        }
        return R.fail("修改失败");
    }




    @GetMapping("/all")
    @ApiOperation(value = "查询所有——币种类型(下拉框)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "币种类型的状态")
    })
    @PreAuthorize("hasAuthority('trade_coin_type_query')")
    public R<List<CoinType>> findAllCoinTypeByStatus(Byte status){
        List<CoinType>  list  = coinTypeService.listByStatus(status);
        return R.ok(list);
    }

















}
